package com.boc.langxiang.ui.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.shalo.ShaloDetailContract;
import com.boc.langxiang.entity.request.AddToShoppingCarRequest;
import com.boc.langxiang.entity.request.ShaloBaomingRequest;
import com.boc.langxiang.entity.response.ShaloDetailEntity;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.ui.activity.common.RegistAndLoginActivity;
import com.boc.langxiang.ui.view.roundedimageview.RoundedImageView;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * CreateTime: 2016-07-07上午11:48
 * Author: wjkjinke00@126.com
 * Description:
 */
public class ShaloBuyDialog extends DialogFragment {

    @BindView(R.id.close)
    ImageView mClose;
    @BindView(R.id.name)
    TextView mName;
    @BindView(R.id.image)
    RoundedImageView mImage;
    @BindView(R.id.money)
    TextView mMoney;
    @BindView(R.id.count)
    TextView mCount;
    @BindView(R.id.left)
    ImageView mLeft;
    @BindView(R.id.shop_car)
    TextView mShopCar;
    @BindView(R.id.buy)
    TextView mBuy;
    private int count = 1;

    private ShaloDetailContract.Presenter mPresenter;
    private ShaloDetailEntity shaloDetailEntity;

    public static ShaloBuyDialog newInstance(ShaloDetailContract.Presenter presenter, ShaloDetailEntity shaloDetailEntity) {
        ShaloBuyDialog dialog = new ShaloBuyDialog();
        dialog.mPresenter = presenter;
        dialog.shaloDetailEntity = shaloDetailEntity;
        return dialog;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Translucent_NoTitleBar);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_shalo_buy, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mCount.setText(count + "");

        mName.setText(StringUtil.getValue(shaloDetailEntity.getTitle()));
        mMoney.setText(StringUtil.getValue(shaloDetailEntity.getPrice()));
        String photourl = shaloDetailEntity.getPhotourl();
        PicassoImageLoader.getInstance().displayUrlImage(getContext(), photourl, mImage);
    }

    @OnClick({R.id.close, R.id.count, R.id.left, R.id.shop_car, R.id.buy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.close:
                dismiss();
                break;
            case R.id.count:
                count++;
                mCount.setText(count + "");
                break;
            case R.id.left:
                if (count == 1) {
                    return;
                }
                count--;
                mCount.setText(count + "");
                break;
            case R.id.shop_car:
                String token = UserInfoManager.getInstance().getToken();
                if (StringUtil.isEmpty(token)) {
                    T.showShort(getActivity(), "请登录!");
                    startActivity(new Intent(getActivity(), RegistAndLoginActivity.class));
                    return;
                }
                AddToShoppingCarRequest request = new AddToShoppingCarRequest();
                request.setTypeId(AddToShoppingCarRequest.SHALO);
                request.setAid(UserInfoManager.getInstance().getUserInfo().getId() + "");
                request.setPid(shaloDetailEntity.getId() + "");
                request.setTitle(shaloDetailEntity.getTitle());
                request.setPhoto(shaloDetailEntity.getPhoto());
                request.setNumber(count + "");
                request.setPrice(shaloDetailEntity.getPrice());
                mPresenter.addToShoppingCar(request);
                dismiss();
                break;
            case R.id.buy:
                ShaloBaomingRequest shaloBaomingRequest = new ShaloBaomingRequest();
                shaloBaomingRequest.id = shaloDetailEntity.getId();
                shaloBaomingRequest.number = mCount.getText().toString();
                mPresenter.shaloBaoming(shaloBaomingRequest);
                dismiss();
                break;
        }
    }
}
